﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using AssessmentActions;

namespace TestingLibrary
{
    public class TestingFunctions
    {
        /// <summary>
        /// deserialize a list of serialized multiple choice questions
        /// </summary>
        /// <param name="questions"></param>
        /// <returns></returns>
        public static List<MultipleChoiceQuestion> deserializeMCList(string[] questions)
        {
            List<MultipleChoiceQuestion> mc_questions = new List<MultipleChoiceQuestion>();

            foreach (string question in questions)
            {
                // Deserialize the question
                MultipleChoiceQuestion mc_question = null;
                byte[] string_bytes = Convert.FromBase64String(question);
                MemoryStream memoryStream = new MemoryStream(string_bytes);
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                mc_question = (MultipleChoiceQuestion)binaryFormatter.Deserialize(memoryStream);
                memoryStream.Close();

                mc_questions.Add(mc_question);
            }

            return mc_questions;
        }

        /// <summary>
        /// deserialize a list of serialized true false questions
        /// </summary>
        /// <param name="questions"></param>
        /// <returns></returns>
        public static List<TrueFalseQuestion> deserializeTFList(string[] questions)
        {
            List<TrueFalseQuestion> tf_questions = new List<TrueFalseQuestion>();

            foreach (string question in questions)
            {
                // Deserialize the question
                TrueFalseQuestion tf_question = null;
                byte[] string_bytes = Convert.FromBase64String(question);
                MemoryStream memoryStream = new MemoryStream(string_bytes);
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                tf_question = (TrueFalseQuestion)binaryFormatter.Deserialize(memoryStream);
                memoryStream.Close();

                tf_questions.Add(tf_question);
            }

            return tf_questions;
        }

        /// <summary>
        /// serialize the list of assessments
        /// </summary>
        /// <param name="list"></param>
        /// <returns>String array of serialized assessments</returns>
        public static string serializeAssessment(Assessment assessment)
        {
            // Serialize the assessment
            MemoryStream memoryStream = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(memoryStream, assessment);
            string serialized_assessment = Convert.ToBase64String(memoryStream.ToArray());

            return serialized_assessment;
        }

        /// <summary>
        /// serialize the list of assessments
        /// </summary>
        /// <param name="list"></param>
        /// <returns>String array of serialized assessments</returns>
        public static Assessment deserializeAssessment(string serialized_assessment)
        {
            // Deserialize the assessment
            Assessment assessment = null;
            byte[] assessmentStringBytes = Convert.FromBase64String(serialized_assessment);
            MemoryStream memoryStream = new MemoryStream(assessmentStringBytes);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            assessment = (Assessment)binaryFormatter.Deserialize(memoryStream);
            memoryStream.Close();

            return assessment;
        }

    }
}
